package com.ny.controller;
import com.ny.entity.Classrooms;
import com.ny.entity.Users;

import com.ny.service.ClassroomService;
import com.ny.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private ClassroomService classroomService;

    @PostMapping("/login")
    public String login(@RequestParam String username, @RequestParam String password, Model model, HttpSession session) {
        Users user = userService.login(username, password);
//        存储用户信息到session中
        session.setAttribute("user", user);

        if (user != null) {
            List<Classrooms> classroomsList = classroomService.findAllClassrooms();
//            classroomsList:代表查询出来的数据。
//            "classroomsList"  model的名字。jsp 就是从中拿取数据。
//            查询出来的数据存放到model中，然后jsp 从model中取出来。jsp就可以获取到数据库查询出来的信息了
            model.addAttribute("classroomsList", classroomsList);
//            model.addAttribute("user", user);
            return "classroom";
        } else {
            model.addAttribute("error", "用户名或密码错误");
            return "login";
        }
    }

    // http:localhost:8082/
// 多条件筛选:  提交的路径:/user/searchClassrooms   2.提交的方法 get  3.请求数据：
    @RequestMapping("/searchClassrooms")
    public String searchClassrooms(@RequestParam String classroomName,
                                   @RequestParam String capacity,
                                   @RequestParam String status,
                                   @RequestParam String building,
                                   @RequestParam String description,
                                   Model model) {

        List<Classrooms> classroomsList = classroomService.searchClassrooms(classroomName, capacity, status, building, description);
        model.addAttribute("classroomsList", classroomsList);
        return "classroom";
    }

    // 处理退出登录请求，这里使用POST方法（也可以根据实际和前端约定使用GET方法等）
    @RequestMapping("/logout")
    public String logout() {
        userService.logout();
        // 退出登录成功后，重定向到登录页面（这里假设登录页面的路径为 /login，可根据实际调整）
        return "redirect:/login";
    }

    @RequestMapping("/myReservations")
    public String myReservations(Model model) {

        return "myReservations";
    }
    @RequestMapping("/personalCenter")
    public String personalCenter() {

        return "personalCenter";
    }
}
